﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface.IColumnManagement;
using IntelligentAgriculture.Domain.Interface.Iridgepletype;
using IntelligentAgriculture.Domain.Interface.SheepFileModelInterface;
using IntelligentAgriculture.Services;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Interface
{
    public class AppearanceModelService : IAppearanceModelService
    {


        //体尺仓储
        private readonly IAppearanceModelRepository appearanceRe;
        private readonly ILogger<BodyMeasureModelService> logger;
        private readonly IMapper imapper;
        //档案仓储
        private readonly ISheepFileModelRepository sheepModelRe;
        //栋舍仓储
        private readonly IridegRepository ridegRe;
        //栏位仓储
        private readonly IColumnManRepository columnRe;

        public AppearanceModelService(IAppearanceModelRepository appearanceRe, ILogger<BodyMeasureModelService> logger, IMapper imapper, ISheepFileModelRepository sheepModelRe, IridegRepository ridegRe, IColumnManRepository columnRe)
        {
            this.appearanceRe = appearanceRe;
            this.logger = logger;
            this.imapper = imapper;
            this.sheepModelRe = sheepModelRe;
            this.ridegRe = ridegRe;
            this.columnRe = columnRe;
        }


        /// <summary>
        /// 品相测定列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageOutput<AppearanceModelOutPut>> AppearanceModellList(AppearanceModelnPut input)
        {


            var listquery = (from x in await appearanceRe.Appearance()
                             join y in await sheepModelRe.GetSheepfilemodels()
                             on x.EarTag equals y.EarTag
                             join z in ridegRe.Showinfo()
                              on x.BuildingManagementId equals z.RidgepoleId
                             join a in columnRe.GetShow()
                             on x.ColumnManagementId equals a.ColumnManagementId
                             where x.LogicalDeletion == 0
                             select new AppearanceModelOutPut
                             {
                                 AppearanceId = x.AppearanceId,
                                 EarTag = x.EarTag,
                                 BuildingManagementId = z.RidgepoleId,
                                 Angle = x.Angle,
                                 ColumnManagementId = a.ColumnManagementId,
                                 CreateBy = x.CreateBy,
                                 CreateDate = x.CreateDate,
                                 FatDistribution = x.FatDistribution,
                                 Hair = x.Hair,
                                 HoofColor = x.HoofColor,
                                 LogicalDeletion = x.LogicalDeletion,
                                 MeasureAge = x.MeasureAge,
                                 MeasureDate = x.MeasureDate,
                                 MouthColor = x.MouthColor,
                                 SheepRescissionSex = x.SheepRescissionSex,
                                 Stain = x.Stain,
                                 Tooth = x.Tooth,
                                 UpdateBy = x.UpdateBy,
                                 UpdateDate = x.UpdateDate,
                                 Variety = x.Variety,
                             });

            if (!string.IsNullOrEmpty(input.earTag))
            {
                listquery = listquery.Where(x => x.EarTag.Contains(input.earTag));
            }
            if (input.variety > 0)
            {
                listquery = listquery.Where(x => x.Variety == input.variety);
            }
            if (input.sheepRescissionSex != null)
            {
                listquery = listquery.Where(x => x.SheepRescissionSex == input.sheepRescissionSex);
            }

            var result = new PageOutput<AppearanceModelOutPut>
            {
                TotalCount = listquery.Count(),
                PageSize = input.PageSize,
            };

            listquery = listquery.OrderBy(c => c.AppearanceId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
            result.Data = imapper.Map<List<AppearanceModelOutPut>>(listquery.ToList());
            return result;

        }




        /// <summary>
        /// 删除品相测定
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> Delete(params int[] ids)
        {
            return await appearanceRe.Delete(ids);

        }


        /// <summary>
        /// 新增品相测定
        /// </summary>
        /// <param name="appearancemodel"></param>
        /// <returns></returns>
        public async Task<int> Add(Appearancemodel appearancemodel)
        {
            var entity = imapper.Map<Appearancemodel>(appearancemodel);
            return await appearanceRe.Add(entity);
        }

        /// <summary>
        /// 更新品相测定
        /// </summary>
        /// <param name="appearancemodel"></param>
        /// <returns></returns>
        public async Task<int> Update(Appearancemodel appearancemodel)
        {
            var entity = imapper.Map<Appearancemodel>(appearancemodel);
            return await appearanceRe.Update(entity);
        }





    }





}
